package com.naizhuai.ui.user.register;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.naizhuai.R;
import com.naizhuai.network.NetWorkObserver;
import com.naizhuai.network.apistore.UserManageDataApiStore;
import com.naizhuai.ui.BaseFragment;
import com.naizhuai.ui.components.ClearWriteEditText;
import com.naizhuai.ui.components.TipDialog;
import com.naizhuai.ui.entity.SendSms;
import com.naizhuai.ui.user.UserManageDataApiStoreIml;
import com.naizhuai.util.NaizhuAILog;
import com.naizhuai.util.ValidateUtil;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.Observer;


public class VerificationCodeFragment extends BaseFragment {
    @BindView(R.id.headToolBar)
    Toolbar headToolBar;
    @BindView(R.id.markword)
    TextView markword;
    @BindView(R.id.ed_phonenumber)
    ClearWriteEditText edPhonenumber;
    @BindView(R.id.btn_login)
    Button btnLogin;
    Unbinder unbinder;
    TipDialog loadingtipDialog;
    public VerificationCodeFragment() {
        // Required empty public constructor
    }



    public static VerificationCodeFragment newInstance() {
        VerificationCodeFragment fragment = new VerificationCodeFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        InitToolBar();
        InitBtnNextStep();
        InitDialog();
    }

    private void InitDialog() {
        if (loadingtipDialog==null){
            loadingtipDialog = new TipDialog.Builder(getActivity())
                    .setIconType(TipDialog.Builder.ICON_LOADING)
                    .setTipText("正在发送")
                    .create();
        }

    }

    private void InitBtnNextStep() {
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            String phonenumber=edPhonenumber.getText().toString().trim();
            if (!ValidateUtil.validateMobile(phonenumber)){
                shortToast(getString(R.string.phone_format_error));
                edPhonenumber.setShakeAnimation();
                return;
             }

            //展示进度请求的ui
            //执行网络请求 取消进度请求 跳转验 证码输入界面,提示错误信息
//                showRegister(phonenumber);
                loadingtipDialog.show();
                UserManageDataApiStore userManageDataApiStore=UserManageDataApiStoreIml.getInstance();
                userManageDataApiStore.sendSms(phonenumber, SendSms.REGISTER_CODE)
                                      .subscribe(new NetWorkObserver<String>() {
                                          @Override
                                          protected void onFail(String errormsg) {
                                              shortToast(errormsg);
                                              loadingtipDialog.dismiss();
                                          }

                                          @Override
                                          protected void onSuccess(String result) {
                                              loadingtipDialog.dismiss();
                                              if ("1".equals(result)){
                                                  showRegister(phonenumber);
                                              } else {
                                                  shortToast(getString(R.string.sendfail));
                                              }
                                          }
                                      });
            }
        });
    }

    private void showRegister(String phonenumber) {
        getFragmentManager().beginTransaction()
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                .replace(R.id.container, RegisterFragment.newInstance(phonenumber))
                .addToBackStack(null)
                .commit();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private void InitToolBar() {
        headToolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_verification_code, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
